import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter_demo_app/http/HttpController.dart';

class MyTest extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MyTestState();
}

class MyTestState extends State<MyTest> with SingleTickerProviderStateMixin {
  var homeTabsController;
  var homeworkList = new List();

  Future getHomeworkData() async {
    // print('getHomeworkData');
    setState(() {
      loadHomeworkList();
    });
  }

  loadHomeworkList() {
    // print('loadHomeworkList');
    Map<String, String> map = new Map();
    map['userId'] = '600915';
    map['workType'] = '0';
    map['pageIndex'] = '1';
    map['pageSize'] = '100';

    HttpController.get('https://www.bettermorph.com/micoservcice/user/homework',
        (data) {
      if (data != null) {
        // print(data);
        final body = json.decode(data.toString()); //接口返回的数据
        final feeds = body['obj'];
        if (body['code'] == 0) {
          //更新变量值
          setState(() {
            homeworkList = feeds;
            // print(homeworkList);
          });
        }
      }
    }, params: map);
  }

  @override
  void initState() {
    homeTabsController = TabController(
      length: homeworkFinishType.length,
      vsync: this, //动画效果的异步处理，默认格式，背下来即可
    );
    super.initState();
    getHomeworkData();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        backgroundColor: Colors.deepOrange,
        elevation: 0.0,
        title: new Text(
          "云练习",
          style: new TextStyle(
              color: Colors.white,
              fontSize: 18.0,
              fontWeight: FontWeight.normal),
        ),
        bottom: new TabBar(
          isScrollable: false,
          controller: homeTabsController,
          indicatorWeight: 2.0,
          indicatorColor: Colors.white,
          tabs: homeworkFinishType.map((Choice choice) {
            return Tab(text: choice.title, icon: Icon(choice.icon));
          }).toList(),
        ),
      ),
      body: new TabBarView(
        controller: homeTabsController,
        children: <Widget>[
          new ListView(
            children: <Widget>[
              new Column(
                children: <Widget>[
                  homework(homeworkList),
                ],
              )
            ],
          ),

          // new Center(child: new Text('全部作业')),
          new Center(child: new Text('待完成')),
          new Center(child: new Text('已完成'))
        ],
      ),
    );
  }

  Widget homework(data) {
    List<Widget> temArrayData = []; //先建一个数组用于存放循环生成的widget
    Widget content;
    for (var item in data) {
      temArrayData.add(new Container(
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            new Row(
              children: <Widget>[
                new Padding(padding: EdgeInsets.all(10.0)),
                new Column(
                  children: <Widget>[
                    fodderTypeJudge('${item["fodderType"]}'),
                  ],
                ),
                new Image.network(
                  '${item["picUrl"]}',
                  width: 100.0,
                  height: 70.0,
                ),
                new Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('${item["taskContent"]}'),
                    Text(
                      '${item["taskContent"]}',
                      style: new TextStyle(fontSize: 12.0, color: Colors.grey),
                    ),
                    Text(
                      '布置时间：${item["releaseTime"]}',
                      style: new TextStyle(fontSize: 12.0, color: Colors.grey),
                    ),
                    Text.rich(TextSpan(children: [
                      TextSpan(
                          text: '已完成人数：',
                          style: new TextStyle(
                              fontSize: 12.0, color: Colors.grey)),
                      TextSpan(
                          text: '${item["count"]}',
                          style: new TextStyle(
                              fontSize: 12.0,
                              color: Color.fromRGBO(241, 86, 51, 1))),
                      TextSpan(
                          text: '人',
                          style: new TextStyle(
                              fontSize: 12.0, color: Colors.grey)),
                    ]))
                  ],
                ),
              ],
            ),
          ],
        ),
      ));
    }
    content = new Column(
        children: temArrayData //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
        //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
        );
    return content;
  }
}

fodderTypeJudge(fodderType) {
  if (fodderType == "教材") {
    return new Column(
      children: <Widget>[
        new Icon(
          Icons.map,
          color: Color.fromRGBO(241, 86, 51, 1),
          size: 20,
        ),
        new Text(
          fodderType,
          style: TextStyle(color: Color.fromRGBO(241, 86, 51, 1), fontSize: 12),
        )
      ],
    );
  } else if (fodderType == "绘本") {
    return new Column(
      children: <Widget>[
        new Icon(
          Icons.perm_media,
          color: Color.fromRGBO(1, 153, 3, 1),
          size: 20,
        ),
        new Text(
          fodderType,
          style: TextStyle(color: Color.fromRGBO(1, 153, 3, 1), fontSize: 12),
        )
      ],
    );
  } else if (fodderType == "跟唱") {
    return new Column(
      children: <Widget>[
        new Icon(
          Icons.music_video,
          color: Color.fromRGBO(254, 139, 35, 1),
          size: 20,
        ),
        new Text(
          fodderType,
          style:
              TextStyle(color: Color.fromRGBO(254, 139, 35, 1), fontSize: 12),
        )
      ],
    );
  }
}

// iconWidget(fodderType){
//   new Column(
//       children: <Widget>[
//         new Icon(
//           fodderTypeJudge(fodderType),
//         ),
//         new Text(
//           fodderType,
//           style: TextStyle(color: Color.fromRGBO(251, 218, 211, 1)),
//         )
//       ],
//     );
// }

class Choice {
  const Choice({this.title, this.icon});
  final String title;
  final IconData icon;
}

const List<Choice> homeworkFinishType = const <Choice>[
  const Choice(title: '全部作业', icon: Icons.sentiment_satisfied),
  const Choice(title: '待完成', icon: Icons.sentiment_very_dissatisfied),
  const Choice(title: '已完成', icon: Icons.sentiment_very_satisfied)
];
